/*
 * game.c
 *
 *  Created on: 15 nov. 2010
 *      Author: Nicolas Aucouturier, Laurent R�veill�re
 */

#include <assert.h>
#include <time.h>

#include "../include/game.h"
#include "../include/map.h"
#include "../include/player.h"
#include "../include/misc.h"
#include "../include/level.h"
#include "../include/bomb.h"
#include "../include/liste.h"
#include "../include/monster.h"

int life_global;

struct s_game {
	t_level curr_level; // current level
	t_player player;
	t_list bomb_list;
	t_monster monster_list;
	t_map curr_map;

	SDL_Surface * number[10];
	SDL_Surface * banner_life;
	SDL_Surface * banner_bomb;
	SDL_Surface * banner_range;
	SDL_Surface * banner_line;

	int current_level;

	int number_set_bomb;			//nombre de bombes déposées qui n'ont pas encore exploser
	int max_bomb;					//nombre de bombes maximales que l'on peut déposer
	int validity;					//variables qui servira à dire si un monstre est "valide" ou non (explication par la suite)


	int variable_chargement;		//To know if the gamer want to load a game

	int destructionn;
	int destructions;
	int destructione;
	int destructiono;
	int coornx;
	int coorny;
	int coorsx;
	int coorsy;
	int coorox;
	int cooroy;
	int coorex;
	int coorey;

	int coormbx;						//Coordonnées d'un monstre "bonus" éventuel
	int coormby;

	//explication de ces nombreuses variables par la suite

};

int num_level_global;

static struct s_game the_game;

t_game game_new(int num_level,int num_life,int init_player_x,int init_player_y,int num_map,int first_time) {
	srand(time(NULL ));

	if(num_level>=0)
		free(the_game.curr_level);

	num_level_global=num_level;

	the_game.curr_level = level_get_level(num_level); // get maps of the current level
	the_game.current_level=num_level;

	if(num_level==1 && num_map==0 && first_time==1)
		{

		the_game.player = player_init(9,9,0,init_player_x,init_player_y,0);

		}

	else
		{

			the_game.player = player_init(9,num_life,num_level,init_player_x,init_player_y,num_map);

		}

	the_game.coormbx=-1;
	the_game.coormby=-1;

	the_game.number_set_bomb=0;
	the_game.max_bomb=3;


	the_game.destructionn=0;
	the_game.destructions=0;
	the_game.destructione=0;
	the_game.destructiono=0;

	the_game.variable_chargement=0;

	the_game.monster_list=monster_init_if_empty(level_get_map(the_game.curr_level, 0),0,0);	//Creation of monster list in memory
	monster_set_number(the_game.monster_list);
	monster_set_validity(the_game.monster_list,0);

	the_game.bomb_list = list_new();

	/* load banner's imgs in memory */
	the_game.number[0] = load_image(IMG_BANNER_0);
	the_game.number[1] = load_image(IMG_BANNER_1);
	the_game.number[2] = load_image(IMG_BANNER_2);
	the_game.number[3] = load_image(IMG_BANNER_3);
	the_game.number[4] = load_image(IMG_BANNER_4);
	the_game.number[5] = load_image(IMG_BANNER_5);
	the_game.number[6] = load_image(IMG_BANNER_6);
	the_game.number[7] = load_image(IMG_BANNER_7);
	the_game.number[8] = load_image(IMG_BANNER_8);
	the_game.number[9] = load_image(IMG_BANNER_9);
	the_game.banner_life = load_image(IMG_BANNER_LIFE);
	the_game.banner_bomb = load_image(IMG_BANNER_BOMB);
	the_game.banner_range = load_image(IMG_BANNER_RANGE);
	the_game.banner_line = load_image(IMG_BANNER_LINE);

	return &the_game;

}

void game_set_numb_level(t_game game)
{
	game->current_level=game->current_level+1;
}

void game_set_validity(t_game game,int x)
{
	game->validity=x;
}

int game_get_validity(t_game game)
{
	return game->validity;
}


void decrease_numb_set_bomb(t_game game)
{
	game->number_set_bomb=game->number_set_bomb-1;
}

/*On met un compteur de destruction pour chaque directions; compteur que l'on incrémente si on trouve un monstre dans l'une de ces direction*/

void increase_game_destructionn(t_game game)
{
	game->destructionn=game->destructionn+1;
}
void increase_game_destructions(t_game game)
{
	game->destructions=game->destructions+1;
}
void increase_game_destructione(t_game game)
{
	game->destructione=game->destructione+1;
}
void increase_game_destructiono(t_game game)
{
	game->destructiono=game->destructiono+1;
}

/*Dans les variables coor, on stoque les coordonnées des monstres se trouvant autours de la bombe au moment de son explosion*/

void change_coorn(t_game game,int x,int y)
{
	game->coornx=x;
	game->coorny=y;
}
void change_cooro(t_game game,int x,int y)
{
	game->coorox=x;
	game->cooroy=y;
}
void change_coors(t_game game,int x,int y)
{
	game->coorsx=x;
	game->coorsy=y;
}
void change_coore(t_game game,int x,int y)
{
	game->coorex=x;
	game->coorey=y;
}
void change_coormb(t_game game,int x,int y)
{
	game->coormbx=x;
	game->coormby=y;
}

void game_free(t_game game) {
	assert(game);

	player_free(game->player);
	level_free(game->curr_level);
}

t_player game_get_player(t_game game) {
	assert(game);
	return game->player;
}

t_monster game_get_monster(t_game game){
	assert(game);
	return game->monster_list;

}

t_list game_get_bomblist(t_game game) {
	assert(game);
	return game->bomb_list;
}


void game_banner_display(t_game game, SDL_Surface* screen) {
	assert(game);
	assert(screen);

	t_map map = level_get_curr_map(game_get_curr_level(game));

	SDL_Rect place;

	int i;
	place.y = (map_get_height(map)) * SIZE_BLOC;
	for (i = 0; i < map_get_width(map); i++) {
		place.x = i * SIZE_BLOC;
		SDL_BlitSurface(game->banner_line, NULL, screen, &place);
	}

	place.y = (map_get_height(map) * SIZE_BLOC) + SIZE_LINE;

	int white_bloc = ((map_get_width(map) * SIZE_BLOC) - 6 * SIZE_BLOC) / 4;

	place.x = white_bloc;
	SDL_BlitSurface(game->banner_life, NULL, screen, &place);
	place.x = white_bloc + SIZE_BLOC;
	// TODO : link lives in banner and lives of player
	SDL_BlitSurface(game->number[player_get_nb_life(game_get_player(game))], NULL, screen, &place);

	place.x = 2 * white_bloc + 2 * SIZE_BLOC;
	SDL_BlitSurface(game->banner_bomb, NULL, screen, &place);
	place.x = 2 * white_bloc + 3 * SIZE_BLOC;
	;
	if (player_get_nb_bomb(game_get_player(game)) >= 0
			&& player_get_nb_bomb(game_get_player(game)) < 10)
		SDL_BlitSurface(game->number[player_get_nb_bomb(game_get_player(game))],
				NULL, screen, &place);

	place.x = 3 * white_bloc + 4 * SIZE_BLOC;
	SDL_BlitSurface(game->banner_range, NULL, screen, &place);
	place.x = 3 * white_bloc + 5 * SIZE_BLOC;
	// TODO : link bomb_range in banner and bomb_range of player
	int bomb_range = player_get_bomb_range(game_get_player(game));
	SDL_BlitSurface(game->number[bomb_range], NULL, screen, &place);
   
   //Number of the level in banner

		place.x = 4 * white_bloc + 5 * SIZE_BLOC;
		// TODO : link bomb_range in banner and bomb_range of player
		int numb_level = player_get_numb_level(game_get_player(game));;
		SDL_BlitSurface(game->number[numb_level], NULL, screen, &place);

}

void game_display(t_game game, SDL_Surface* screen) {
	assert(game);

	t_player player = game_get_player(game);

	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
	game_banner_display(game, screen);
	level_display(game_get_curr_level(game), screen);

	if(num_level_global!=4)
		player_display(game->player, screen);



	if(game_get_monster(game)!=NULL)
		{
		monster_display(game->monster_list,screen);
		}



	if(map_get_cell_type(level_get_map(the_game.curr_level, 0), player_get_x(player),player_get_y(player))==CELL_KEY)
	{
		map_set_cell_type(level_get_map(the_game.curr_level, 0), player_get_x(player),player_get_y(player),CELL_EMPTY);
	}






 	/*//if (list_print_bomb(game->bomb_list) != NULL) {													//list_is_empty(game->bomb_list)) {
		//while (list_print_bomb(game->bomb_list) != NULL) {
		//	display_bomb(list_print_bomb(game->bomb_list), screen);
			//game->bomb_list = list_print_next(game->bomb_list);
		//}
	//}*/
	//int list_number=get_bomb_number(game->bomb_list);
	if (list_print_bomb(game->bomb_list) != NULL && list_print_next(game->bomb_list) != NULL) {
		while (list_print_bomb(game->bomb_list) != NULL && list_print_next(game->bomb_list) != NULL) {
				int explo=update_bomb_status (list_print_bomb(game->bomb_list),game);
				if (explo == 1) {
					bomb_explosion(list_print_bomb(game->bomb_list),game,screen);
					game->bomb_list=remove_from_list(game_get_bomblist(game));

				}
				if (list_print_next(game->bomb_list) != NULL){
					game->bomb_list=list_print_next(game->bomb_list);
					}

			}
		while (list_print_prec(game->bomb_list) != NULL) {
				game->bomb_list=list_print_prec(game->bomb_list);
			}
		while (list_print_bomb(game->bomb_list) != NULL && list_print_next(game->bomb_list) != NULL) {
					display_bomb(list_print_bomb(game->bomb_list),screen);
					game->bomb_list=list_print_next(game->bomb_list);
			}
		while (list_print_prec(game->bomb_list) != NULL) {
							game->bomb_list=list_print_prec(game->bomb_list);
			}



	}
		//update_list_images(game->bomb_list,screen);

	SDL_Flip(screen); // SDL_Flip -- swaps screen buffers -- update
}

t_level game_get_curr_level(t_game game) {
	return game->curr_level;
}

uint input_update(t_game game) {
	SDL_Event event;
	t_player player = game_get_player(game);
	t_map map = level_get_curr_map(game_get_curr_level(game));
	FILE* sauvegarde=NULL;


	while (SDL_PollEvent(&event)) {
		switch (event.type) {
		case SDL_QUIT:
			return 1;
		case SDL_KEYDOWN:

			switch (event.key.keysym.sym) {
			case SDLK_ESCAPE:
				return 1;

				/*if num_level_global=4, we are in the last level and we don't want that the gammer can move on the last map*/
			case SDLK_UP:

				if(num_level_global!=4){
				player_set_current_way(player, NORTH);
				player_move(player, map);
				}
				break;
			case SDLK_DOWN:

				if(num_level_global!=4){
				player_set_current_way(player, SOUTH);
				player_move(player, map);
				}
				break;
			case SDLK_RIGHT:

				if(num_level_global!=4){
				player_set_current_way(player, EAST);
				player_move(player, map);
				}
				break;
			case SDLK_LEFT:

				if(num_level_global!=4){
				player_set_current_way(player, WEST);
				player_move(player, map);}
				break;

			case SDLK_c:

				game->variable_chargement=1;
				break;

			case SDLK_e:  //the player want to save his game

				sauvegarde=fopen("sauvegarde.txt","w");
				if(sauvegarde!=NULL)
				{
					//we write in a file named sauvegarde.txt all the main information of the current game of the gamer
					fprintf(sauvegarde,"%d %d %d %d %d %d %d %d",player_get_nb_bomb(player),player_get_nb_life(player),player_get_key(player),
							player_get_x(player),player_get_y(player),player_get_bomb_range(player),player_get_numb_level(player),
							player_get_numb_map(player));
				}
				else
					printf("erreur lors de l'enregistrement de la partie");
				fclose(sauvegarde);
				break;


			case SDLK_SPACE:
				// TODO Set a bomb

				if(num_level_global!=4){

				if(game->number_set_bomb<game->max_bomb){
				if (player_get_nb_bomb(game->player) > 0 && map_get_cell_type(map, player_get_x(player), player_get_y(player))!=CELL_DOOR && map_get_cell_type(map, player_get_x(player), player_get_y(player))!=CELL_MONSTER && map_get_cell_type(map, player_get_x(player), player_get_y(player))!=CELL_BOMB ) {
					map_set_cell_type(map, player_get_x(player),player_get_y(player),CELL_BOMB);
				    game->bomb_list=list_add(game->bomb_list,new_bomb(player,SDL_GetTicks()));
				    game->number_set_bomb=game->number_set_bomb+1;
				    //game->monster_list=delete_monster(map,game->monster_list);
				    player_dec_nb_bomb(player);
				}}}

				break;

			default:
				break;
			}

			break;
		}
	}
	return 0;
}

int game_update(t_game game) {
	int rand_numb;
	t_monster monster = game_get_monster(game);
	t_map map = level_get_curr_map(game_get_curr_level(game));
	t_level level=game_get_curr_level(game);
	t_player player = game_get_player(game);
	life_global=player_get_nb_life(player);

	if (input_update(game) == 1) {
		return 1; // exit game
	}

	if(game->variable_chargement==1)	//the player want to load a game
	{
		int read=0;
		FILE* chargement=NULL;
		int i=0;
		chargement=fopen("sauvegarde.txt","r");
		int load[8]={0};
		for(i=0;i<8;i++)
			{
				fscanf(chargement,"%d ",&read);
				load[i]=(int) read;
				printf("%d--",load[i]);
			}
		fclose(chargement);

		game_free(game);
		game=game_new(load[6],load[1],load[3],load[4],load[7],0);		//new game with information of the game loaded
		player=game_get_player(game);
		player_set_var_map(player,player_get_numb_map(player));

		change_current_map(level,player);								//Loading of the map of the player when he saved his game
		player_set_var_map(player,-1);
		if(load[2]==1)
			inc_player_key(player);
		player_set_bomb_range(player,load[5]);
		player_set_nb_bomb(player,load[0]);




			switch(player_get_numb_level(player)){						//Management of the monsters on the game loaded (we put monster as if it was the first time of the player on this map)
					case 3:
						if(player_get_numb_map(player)==1)
						{
							game->monster_list=new_monster(0,1,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 0,1,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
							game->monster_list=new_monster(11,6,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 11,6,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
							game->monster_list=new_monster(1,6,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 1,6,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
						}
						if(player_get_numb_map(player)==2)
						{
							game->monster_list=new_monster(6,0,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 6,0,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
							game->monster_list=new_monster(11,6,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 11,6,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
							game->monster_list=new_monster(1,6,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 1,6,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
							game->monster_list=new_monster(11,0,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 11,0,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
							game->monster_list=new_monster(3,0,the_game.monster_list);
							monster_set_number(game->monster_list);
							map_set_cell_type(level_get_map(game->curr_level, 0), 3,0,CELL_MONSTER);
							monster_set_validity(game->monster_list,1);
						}
						break;
						case 2:
							if(player_get_numb_map(player)==1)
							{
								game->monster_list=new_monster(11,1,the_game.monster_list);
								monster_set_number(game->monster_list);
								map_set_cell_type(level_get_map(game->curr_level, 0), 11,1,CELL_MONSTER);
								monster_set_validity(game->monster_list,1);
							}
							if(player_get_numb_map(player)==2)
							{
								game->monster_list=new_monster(0,0,the_game.monster_list);
								monster_set_number(game->monster_list);
								map_set_cell_type(level_get_map(game->curr_level, 0), 0,0,CELL_MONSTER);
								monster_set_validity(game->monster_list,1);
								game->monster_list=new_monster(0,4,the_game.monster_list);
								monster_set_number(game->monster_list);
								map_set_cell_type(level_get_map(game->curr_level, 0), 0,4,CELL_MONSTER);
								monster_set_validity(game->monster_list,1);
								game->monster_list=new_monster(1,6,the_game.monster_list);
								monster_set_number(game->monster_list);
								map_set_cell_type(level_get_map(game->curr_level, 0), 1,6,CELL_MONSTER);
								monster_set_validity(game->monster_list,1);

							}
							break;
						case 1:
							if(player_get_numb_map(player)==2)
							{
								game->monster_list=new_monster(0,6,the_game.monster_list);
								monster_set_number(game->monster_list);
								map_set_cell_type(level_get_map(game->curr_level, 0), 0,6,CELL_MONSTER);
								monster_set_validity(game->monster_list,1);

							}
							if(player_get_numb_map(player)==1)
							{
								game->monster_list=new_monster(7,6,the_game.monster_list);
								monster_set_number(game->monster_list);
								map_set_cell_type(level_get_map(game->curr_level, 0), 7,6,CELL_MONSTER);
								monster_set_validity(game->monster_list,1);
							}
							break;
			}
	}
	if(player_get_nb_life(player)==0)
	{
		printf("GAME OVER");
		return 1;
	}

	if(game->destructionn==1)
	{
		game->monster_list=delete_monster(game,map,game->coornx,game->coorny,game_get_monster(game));
		monster_set_validity(monster,game->validity);

		game->destructionn=0;
		game->validity=1;
	}
	/*Si destructionn vaut 1 cela veut dire que l'on a un monstre au nord de la bombe au moment de son explosion. On supprime donc ce monstres
	 * de la liste.
	 */

	if(game->destructions==1)
	{
		game->monster_list=delete_monster(game,map,game->coorsx,game->coorsy,game_get_monster(game));
		monster_set_validity(monster,game->validity);

		game->destructions=0;
		game->validity=1;
	}
	if(game->destructione==1)
	{
		game->monster_list=delete_monster(game,map,game->coorex,game->coorey,game_get_monster(game));
		monster_set_validity(monster,game->validity);

		game->destructione=0;
		game->validity=1;
	}
	if(game->destructiono==1)
	{
		game->monster_list=delete_monster(game,map,game->coorox,game->cooroy,game_get_monster(game));
		monster_set_validity(monster,game->validity);

		game->destructiono=0;
		game->validity=1;
	}

	if(player_get_var_map(player)!=-1)
	{

		map=map_get_bomb_removed(map);
		game->number_set_bomb=0;
		change_current_map(level,player);
		game->monster_list=delete_all_monster(game,map,monster);
		game->bomb_list=list_new();

		//Placement of the monsters of the current map
		switch(player_get_numb_level(player)){
		case 3:
			if(player_get_numb_map(player)==1)
			{
				game->monster_list=new_monster(0,1,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 0,1,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
				game->monster_list=new_monster(11,6,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 11,6,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
				game->monster_list=new_monster(1,6,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 1,6,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
			}

			if(player_get_numb_map(player)==2)
			{
				game->monster_list=new_monster(6,0,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 6,0,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
				game->monster_list=new_monster(11,6,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 11,6,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
				game->monster_list=new_monster(1,6,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 1,6,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
				game->monster_list=new_monster(11,0,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 11,0,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
				game->monster_list=new_monster(3,0,the_game.monster_list);
				monster_set_number(game->monster_list);
				map_set_cell_type(level_get_map(game->curr_level, 0), 3,0,CELL_MONSTER);
				monster_set_validity(game->monster_list,1);
			}
			break;
			case 2:
				if(player_get_numb_map(player)==1)
				{
					game->monster_list=new_monster(11,1,the_game.monster_list);
					monster_set_number(game->monster_list);
					map_set_cell_type(level_get_map(game->curr_level, 0), 11,1,CELL_MONSTER);
					monster_set_validity(game->monster_list,1);
				}
				if(player_get_numb_map(player)==2)
				{
					game->monster_list=new_monster(0,0,the_game.monster_list);
					monster_set_number(game->monster_list);
					map_set_cell_type(level_get_map(game->curr_level, 0), 0,0,CELL_MONSTER);
					monster_set_validity(game->monster_list,1);
					game->monster_list=new_monster(0,4,the_game.monster_list);
					monster_set_number(game->monster_list);
					map_set_cell_type(level_get_map(game->curr_level, 0), 0,4,CELL_MONSTER);
					monster_set_validity(game->monster_list,1);
					game->monster_list=new_monster(1,6,the_game.monster_list);
					monster_set_number(game->monster_list);
					map_set_cell_type(level_get_map(game->curr_level, 0), 1,6,CELL_MONSTER);
					monster_set_validity(game->monster_list,1);

				}
				break;
			case 1:
				if(player_get_numb_map(player)==2)
				{
					game->monster_list=new_monster(0,6,the_game.monster_list);
					monster_set_number(game->monster_list);
					map_set_cell_type(level_get_map(game->curr_level, 0), 0,6,CELL_MONSTER);
					monster_set_validity(game->monster_list,1);
				}
				if(player_get_numb_map(player)==1)
				{
					game->monster_list=new_monster(7,6,the_game.monster_list);
					monster_set_number(game->monster_list);
					map_set_cell_type(level_get_map(game->curr_level, 0), 7,6,CELL_MONSTER);
					monster_set_validity(game->monster_list,1);
				}
				break;

		}
		reinitialise_player_var_map(player);

	}
	if((player_get_numb_level(player)-player_get_numb_level_old(player))!=0)
	{
		game_free(game);
		game=game_new(player_get_numb_level(player),life_global,0,0,0,0);
	}


	// TODO monsters management ...

	//Management of monster's bonus; we create a new monster at the place of the case
	if(game->coormbx!=-1 && game->coormby!=-1)
	{
		game->monster_list=new_monster(game->coormbx,game->coormby,the_game.monster_list);
		monster_set_number(game->monster_list);
		map_set_cell_type(level_get_map(game->curr_level, 0), game->coormbx,game->coormby,CELL_MONSTER);
		monster_set_validity(game->monster_list,1);
		change_coormb(game,-1,-1);
	}

	while(monster!=NULL)
		{
		if(monster_get_validity(monster)!=0){
		if(SDL_GetTicks()- get_last_state(monster) > 1000)
			{
			rand_numb=rand()%4;


			if(rand_numb==3)
			{
				monster_set_current_way(monster,NORTH);
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
				monster_move(monster,map);
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER);

				set_last_state(monster);	//We change the timer of the monster; we get the moment of his last movement
			}
			if(rand_numb==2)
			{
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
				monster_set_current_way(monster,SOUTH);
				monster_move(monster, map);
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER);

				set_last_state(monster);
			}
			if(rand_numb==1)
			{
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
				monster_set_current_way(monster,EAST);
				monster_move(monster, map);
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER);

				set_last_state(monster);

			}
			if(rand_numb==0)
			{
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_EMPTY);
				monster_set_current_way(monster,WEST);
				monster_move(monster, map);
				map_set_cell_type(map,monster_get_x(monster),monster_get_y(monster),CELL_MONSTER);

				set_last_state(monster);


			}
			//if the player walk onto a monster
			if(player_get_x(player)==monster_get_x(monster) && player_get_y(player)==monster_get_y(monster) && player_get_monster_or_not(player)==0)
				player_dec_nb_life(player);
			}



		}
		if(monster_get_next(monster)!=NULL)			//Checking if the monster is the last in the list
			monster=monster_get_next(monster);

		else
			break;			//If it was the last monster in the list, we quit the while
		}


		monster=monster_init_list(monster);

	return 0;
}
